/** @format */

import { ref } from 'vue'
import { UserService } from '../services/userService'
import { useEntity } from './useEntity'

const userService = new UserService()
const openId = ref()

const getOpenId = async () => {
  openId.value = await userService.getOpenId()
}

export const useShare = (id, params: any = {}) => {
  const { share = false, oid } = params

  if (!share) {
    getOpenId()
  }

  const options = {
    cache: false,
    path: async () => {
      // 如果是分享则直接返回
      if (share) return `share/${oid}/${id}`
      // 否则取得用户信息
      await getOpenId()
      return `share/${openId.value}/${id}`
    },
  }
  const { content, getData, updateData } = useEntity(options)
  return {
    openId,
    content,
    getData,
    updateData,
  }
}
